package com.dlyk.service.impl;

import com.alibaba.excel.EasyExcel;
import com.dlyk.bean.TClue;
import com.dlyk.constant.Constants;
import com.dlyk.listener.UploadDataListener;
import com.dlyk.mapper.TClueMapper;
import com.dlyk.query.BaseQuery;
import com.dlyk.query.ClueQuery;
import com.dlyk.result.R;
import com.dlyk.service.ClueService;
import com.dlyk.utils.JWTUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class ClueServiceImpl implements ClueService {

    @Autowired
    private TClueMapper clueMapper;

    /**
     * 线索管理分页查询
     * @param current
     * @return
     */
    @Override
    public PageInfo<TClue> getClueByPage(Integer current) {
        // 开始分页
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        // 查询
        List<TClue> list=clueMapper.selectByCluePage(BaseQuery.builder().build());
        // 封装
        PageInfo<TClue> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 导入Excel文件
     * @param inputStream
     */
    @Override
    public void importExcel(InputStream inputStream,String token) {

        // inputStream 输入流
        // TClue.class 模板类
        // new DemoDataListener() 监听器
        EasyExcel.read(inputStream, TClue.class, new UploadDataListener(clueMapper,token))
                .sheet()
                .doRead();
    }

    /**
     * 验证手机号是否录入
     * @param phone
     * @return
     */
    @Override
    public Boolean getPhone(String phone) {
        int count = clueMapper.getPhone(phone);
        return count<=0;
    }

    /**
     * 录入线索
     * @param clueQuery
     * @return
     */
    @Override
    public int save(ClueQuery clueQuery) {

        int count = clueMapper.getPhone(clueQuery.getPhone());
        if (count<0){
            TClue clue = new TClue();
            BeanUtils.copyProperties(clueQuery,clue);

            Integer id = JWTUtils.parseUserFromJWT(clueQuery.getToken()).getId();
            clue.setId(id);

            clue.setCreateTime(new Date());  // 创建时间
            clue.setCreateBy(id);  // 创建人
            return clueMapper.insertSelective(clue);
        }else {
            throw new RuntimeException("该手机号已经录入，无法再录入");
        }
    }

    /**
     * 根据id查询线索详情
     * @param id
     * @return
     */
    @Override
    public TClue getClueById(Integer id) {
        return clueMapper.selectDetailById(id);
    }

    /**
     * 修改线索数据
     * @param clueQuery
     * @return
     */
    @Override
    public int updateClue(ClueQuery clueQuery) {
        TClue clue = new TClue();
        BeanUtils.copyProperties(clueQuery,clue);

        Integer id = JWTUtils.parseUserFromJWT(clueQuery.getToken()).getId();
        clue.setId(id);

        clue.setEditTime(new Date());  // 创建时间
        clue.setEditBy(id);  // 创建人
        return clueMapper.updateByPrimaryKeySelective(clue);
    }

    /**
     * 删除线索
     * @param id
     * @return
     */
    @Override
    public int deleteClue(Integer id) {
        return clueMapper.deleteByPrimaryKey(id);
    }
}
